package com.etonedu.meals.http.xutils;

import android.text.TextUtils;
import android.util.Log;

import com.etonedu.meals.utils.Utils;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class XUtilsRequest {

    private XUtilsResponse xUtilsResponse;

    public void setxUtilsResponse(XUtilsResponse xUtilsResponse) {
        this.xUtilsResponse = xUtilsResponse;
    }

    public void request(String url, HttpMethod method, HashMap<String, Object> params) {

        String paramStr = "";
        if (method == HttpMethod.GET) {
            url = url + "?" + paramstoString(params);
            Log.d("XUtilsRequest","url = "+url);
        } else if (method == HttpMethod.POST) {
            Gson gson = new Gson();
            paramStr = gson.toJson(params);
            Log.d("XUtilsRequest","paramStr = "+paramStr);
        }

        RequestParams requestParams = new RequestParams(url);
        if (!TextUtils.isEmpty(paramStr)) {
            requestParams.setBodyContent(paramStr);
        }

        final String finalUrl = url;
        x.http().request(method, requestParams, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                Log.d("XUtilsRequest","url = "+ finalUrl +" , "+result);
                if (xUtilsResponse != null) {
                    xUtilsResponse.response(result);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("XUtilsRequest", Utils.getLineInfo()+",ex = "+ex.getMessage());
                if (xUtilsResponse != null) {
                    xUtilsResponse.error(ex.getMessage());
                }
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    // 表单请求
    public void formRequest(final String url, HttpMethod method, HashMap<String, String> params) {

        RequestParams requestParams = new RequestParams(url);
        requestParams.setConnectTimeout(15 * 1000);
        if (params != null && params.size() > 0) {
            Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                requestParams.addBodyParameter(entry.getKey(),entry.getValue());
            }
        }

        x.http().request(method, requestParams, new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                Log.d("XUtilsRequest","url = "+ url +" , "+result);
                if (xUtilsResponse != null) {
                    xUtilsResponse.response(result);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("XUtilsRequest", Utils.getLineInfo()+",ex = "+ex.getMessage());
                ex.printStackTrace();
                if (xUtilsResponse != null) {
                    xUtilsResponse.error(ex.getMessage());
                }
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });


    }

    private String paramstoString(Map<String, Object> params) {
        if (params != null && params.size() > 0) {
            StringBuilder encodedParams = new StringBuilder();
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                encodedParams.append(entry.getKey());
                encodedParams.append('=');
                encodedParams.append(entry.getValue());
                encodedParams.append('&');

            }
            return encodedParams.substring(0,encodedParams.length()-1);
        }
        return null;
    }
}
